﻿#region 命名空间

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.Task
{
    [Access("266002")]
    public partial class Add : WebFactory.PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["ID"] != null)
                {
                    Edit_Load();
                }
            }
        }

        #region 激活类型

        /// <summary>
        ///     激活类型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RBL_TaskActive_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (this.rblTaskActive.SelectedIndex)
            {
                case 0:
                    this.Panel1.Visible = true;
                    this.Panel2.Visible = false;
                    this.Panel3.Visible = false;
                    this.Panel4.Visible = false;

                    break;
                case 1:
                    this.Panel1.Visible = false;
                    this.Panel2.Visible = true;
                    this.Panel3.Visible = false;
                    this.Panel4.Visible = false;

                    break;
                case 2:
                    this.Panel1.Visible = false;
                    this.Panel2.Visible = false;
                    this.Panel3.Visible = true;
                    this.Panel4.Visible = false;

                    break;
                case 3:
                    this.Panel1.Visible = false;
                    this.Panel2.Visible = false;
                    this.Panel3.Visible = false;
                    this.Panel4.Visible = true;

                    break;
            }
        }

        #endregion

        protected void btnOK_Click(object sender, EventArgs e)
        {
            AddUI();
        }

        #region 添加或修改

        private void AddUI()
        {
            var model = new WinnerCMS.Model.Task
            {
                TaskName = this.txtTaskName.Text.Trim(),
                TaskActiveId = int.Parse(this.rblTaskActive.SelectedValue),
                AwakenTime = null,
                IntervalMinute = -1,
                Week = 0,
                EveryDayTime = null
            };


            switch (this.rblTaskActive.SelectedIndex)
            {
                case 0:
                    model.AwakenTime =
                        DateTime.Parse(string.Concat(this.txtRunDate.Text, " ", this.txtRunTime.Text.Trim()));
                    break;
                case 1:

                    model.IntervalMinute = int.Parse(this.txtIntervalMinute.Text);
                    break;
                case 2:
                    model.EveryDayTime = this.txtRunTime2.Text.Trim();

                    break;
                case 3:
                    model.AwakenTime = DateTime.Parse(string.Concat("1949-10-01 ", this.txtRunTime3.Text.Trim()));
                    //只是用AwakenTime字段保存时间
                    model.Week = int.Parse(this.RBL_Week.SelectedValue);
                    break;
            }

            model.CommandLine = this.txtCommandLine.Text;
            model.UnexecuteType = int.Parse(this.rblUnexecuteType.SelectedValue);
            model.IsUsed = bool.Parse(this.rblIsUsed.SelectedValue);
            model.DLLName = this.txtDLLName.Text.Trim();
            model.FullClassName = this.txtFullCategoryName.Text.Trim();
            if (Request["ID"] == null)
            {
                model.Id = 0;
            }
            else
            {
                model.Id = int.Parse(Request["ID"]);
            }

            var res = new TaskManage().Add(model);

            if (string.IsNullOrEmpty(res.Errors) == false)
            {
                MsgBox.Message(res.Errors, this);
            }
            else
            {
                MsgBox.Message("操作完成！", "Manage.aspx", this);
            }
        }

        #endregion

        #region 加载

        private void Edit_Load()
        {
            var model = new TaskManage().GetInfo(Request["ID"]);
            if (model != null)
            {
                this.txtTaskName.Text = model.TaskName;
                this.rblTaskActive.SelectedValue = model.TaskActiveId.ToString();

                RBL_TaskActive_SelectedIndexChanged(null, null);

                switch (this.rblTaskActive.SelectedIndex)
                {
                    case 0:
                        this.txtRunDate.Text = Convert.ToDateTime(model.AwakenTime).ToShortDateString();
                        this.txtRunTime.Text = Convert.ToDateTime(model.AwakenTime).ToString("HH:mm:ss");
                        break;
                    case 1:
                        this.txtIntervalMinute.Text = model.IntervalMinute.ToString();
                        break;
                    case 2:
                        this.txtRunTime2.Text = model.EveryDayTime;
                        break;
                    case 3:
                        this.txtRunTime3.Text = Convert.ToDateTime(model.AwakenTime).ToString("HH:mm:ss");
                        this.RBL_Week.SelectedValue = model.Week.ToString();
                        break;
                }
                this.rblUnexecuteType.SelectedValue = model.UnexecuteType.ToString();
                this.rblIsUsed.SelectedValue = model.IsUsed.ToString();
                this.txtDLLName.Text = model.DLLName;
                this.txtFullCategoryName.Text = model.FullClassName;
                this.txtCommandLine.Text = model.CommandLine;
            }
            else
            {
                //转向错误页面
                ErrClass.Redirect("数据不存在。");
            }
        }

        #endregion
    }
}